<?php
namespace Cyfy\Modules\QuickLinks\Forms;
use \Cyfy\Message;
use \Cyfy\Modules\Forms;

/**
 * Creates a new QuickLink.
 */
class Create extends Forms\Form
{
	function structure()
	{		
		$this -> permission("QuickLinks", "create and edit links");
		
		$i = new Forms\TextBox("name", self :: t(110));
		$i -> description = self :: t(120);
		$i -> required = true;
		$i -> maxlength = 100;
		$this -> addItem($i);
		
		$i = new Forms\TextBox("code", self :: t(130));
		$i -> description = self :: t(140);
		$i -> required = true;
		$i -> maxlength = 100;
		$this -> addItem($i);
		
		$i = new Forms\TextBox("link", self :: t(150));
		$i -> required = true;
		$i -> maxlength = 100;
		$this -> addItem($i);
		
		$i = new Forms\TextBox("text", self :: t(170));
		$i -> required = true;
		$i -> maxlength = 255;
		$this -> addItem($i);
		
		$i = new Forms\TextBox("excludeFrom", self :: t(180));
		$i -> description = self :: t(190);
		$i -> maxlength = 255;
		$this -> addItem($i);
		
		
		$b = new Forms\Button(self :: t(210), Forms\Button::OK);
		
		$this -> onSubmit = "Cyfy.ajax('QuickLinks', 'list', '#AdminListOfQuickLinks', '');";
		$this -> addButton($b);
    }
  
	function validate()
	{
		if(preg_match("/^[a-z-]+$/", $this->get("code")) == false)
		{
			\Cyfy\Message :: set(self :: t(140), \Cyfy\Message::ERROR);
			return false;
		}
		return true;	
	}
  
	function submit()
	{  
        
		if(\dibi :: query("INSERT INTO [:cyfy:QuickLinks] %v", $this -> getAll()))
        {
			\Cyfy\Message :: set(self :: t(201, array($this->get("name"))));
            return true;     
		}	
		
        \Cyfy\Message :: set(self :: t(211, array($this->get("name"))), \Cyfy\Message::ERROR);
	}	
}